<template>
  <div class="test">
    <el-form ref="ruleForm" :model="formData" :rules="rules" class="demo-ruleForm">
      <el-form-item label="手机号：" prop="userName">
        <el-input v-model="formData.userName" />
      </el-form-item>
      <el-form-item label="密码：" prop="pass">
        <el-input v-model="formData.pass" type="password" />
      </el-form-item>
      <el-form-item label="请再次输入密码：" prop="checkPass">
        <el-input v-model="formData.checkPass" type="password" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">立即创建</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    const checkPass = (rules, val, callback) => {
      const isSamePass = val === this.formData.pass
      if (isSamePass) {
        callback()
      } else {
        callback(new Error('两次密码必须相同'))
      }
    }
    return {
      formData: {
        userName: '',
        pass: '',
        checkPass: ''
      },
      rules: {
        userName: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          { pattern: /^(?:(?:\+|00)86)?1[3-9]\d{9}$/, message: '请输入11位正确的手机号', trigger: 'blur' }
        ],
        pass: [
          { required: true, message: '该项不能为空', trigger: 'blur' },
          // min => 最小长度 max => 最大长度
          { min: 6, max: 12, message: '密码在 6-12 位之间', trigger: 'blur' }
        ],
        checkPass: [
          { required: true, message: '两次密码必须相同', trigger: 'blur' },
          { validator: checkPass, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    onSubmit() {
      console.log(this.formData)
    }
  }
}
</script>

<style>

</style>
